package com.runchinaup.smartbelt.view.pop;

import android.content.Context;
import android.graphics.drawable.PaintDrawable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runchinaup.runchilibrary.utils.ViewUtil;
import com.runchinaup.smartbelt.R;

import java.util.ArrayList;
import java.util.List;

import static com.runchinaup.smartbelt.App.getApp;

/**
 * Created by liber on 2018/1/29.
 */

public class ListPop {

    private Context context;
    private View contentView;
    private ListPopClickCallback clickCallback;
    private PopupWindow popupWindow;

    private List<SecondaryListAdapter.DataTree<String, String>> traditionList = new ArrayList<>();
    private List<SecondaryListAdapter.DataTree<String, String>> modernList = new ArrayList<>();


    private ListPop(Context context, String bluetoothElectrivity) {
        this.context = context;
        initViewEvent(context, bluetoothElectrivity);

    }

    private volatile static ListPop pop;

    public static ListPop getPop(Context context, String bluetoothElectrivity) {
        synchronized (Void.class) {
            if (pop == null) {
                synchronized (Void.class) {
                    pop = new ListPop(context, bluetoothElectrivity);
                }
            }
        }
        return pop;
    }


    public ListPop showPicker(View view, String text, ListPopClickCallback popClickCallback) {
        this.clickCallback = popClickCallback;
        popupWindow.showAtLocation(view, Gravity.RIGHT, 0, ViewUtil.dip2px(context, 64));
        backgroundAlpha(0.5f);
        return this;
    }

    private TextView eleText;

    private void initViewEvent(Context context,String bluetoothElectrivity) {
        contentView = LayoutInflater.from(context).inflate(R.layout.pop_list, null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setBackgroundDrawable(new PaintDrawable());         //如果不设置，可能对应的popWindow没有"外面"可以点击,导致返回键失效
        popupWindow.setAnimationStyle(R.style.anim_menu_RightBar);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                dismiss();
                backgroundAlpha(1f);
            }
        });
        eleText = contentView.findViewById(R.id.eleText);
        eleText.setText(bluetoothElectrivity + "%");
        int ele = Integer.parseInt(bluetoothElectrivity);
        if (ele > 20) {
            eleText.setTextColor(0xff7ED321);
        } else {
            eleText.setTextColor(0xffff1633);
        }

        //传统理疗
        final TextView traditionPhysical = contentView.findViewById(R.id.tradition_physical);
        //现代理疗
        final TextView modernPhysical = contentView.findViewById(R.id.modern_physical);

        traditionPhysical.setTextColor(0xffFD0489);
        modernPhysical.setTextColor(0xff333333);

        final RecyclerView traditionRecyclerView = contentView.findViewById(R.id.tradition_recyclerview);
        traditionRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        traditionRecyclerView.setHasFixedSize(true);
        RecyclerViewAdapter traditionAdapter = new RecyclerViewAdapter(context) {
            @Override
            public void OnSubItemClick(String tvContent) {
                if (clickCallback != null) {
                    clickCallback.onSelect(tvContent);
                    clickCallback.chooseTradition();
                    dismiss();
                }
            }
        };
        traditionAdapter.setData(traditionList);
        traditionRecyclerView.setAdapter(traditionAdapter);

        final RecyclerView modernRecyclerView = contentView.findViewById(R.id.modern_recyclerview);
        modernRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        modernRecyclerView.setHasFixedSize(true);
        RecyclerViewAdapter modernAdapter = new RecyclerViewAdapter(context) {
            @Override
            public void OnSubItemClick(String tvContent) {
                if (clickCallback != null) {
                    clickCallback.onSelect(tvContent);
                    clickCallback.chooseModern();
                    dismiss();
                }
            }
        };
        modernAdapter.setData(modernList);
        modernRecyclerView.setAdapter(modernAdapter);

        traditionPhysical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modernRecyclerView.setVisibility(View.GONE);
                traditionRecyclerView.setVisibility(View.VISIBLE);
                traditionPhysical.setTextColor(0xffFD0489);
                modernPhysical.setTextColor(0xff333333);
            }
        });

        modernPhysical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                traditionRecyclerView.setVisibility(View.GONE);
                modernRecyclerView.setVisibility(View.VISIBLE);
                traditionPhysical.setTextColor(0xff333333);
                modernPhysical.setTextColor(0xffFD0489);
            }
        });

    }

    //关闭弹窗
    public void dismiss() {
        popupWindow.dismiss();
        backgroundAlpha(1f);
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((FragmentActivity) context).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((FragmentActivity) context).getWindow().setAttributes(lp);
    }


    public String $str(int id) {
        context = getApp();
        return context.getString(id);
    }

    {
        //中药
        traditionList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.traditional_medicine), new ArrayList<String>() {{
            add($str(R.string.traditional_medicine_one));
            add($str(R.string.traditional_medicine_two));
            add($str(R.string.traditional_medicine_three));
            add($str(R.string.traditional_medicine_four));
            add($str(R.string.traditional_medicine_five));
            add($str(R.string.traditional_medicine_six));
        }}));
        //盲人
        traditionList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.blind_man), new ArrayList<String>() {{
            add($str(R.string.blind_man_one));
            add($str(R.string.blind_man_two));
            add($str(R.string.blind_man_three));
            add($str(R.string.blind_man_four));
            add($str(R.string.blind_man_five));
            add($str(R.string.blind_man_six));
        }}));
        //仿真
        traditionList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.simulation), new ArrayList<String>() {{
            add($str(R.string.simulation_one));
            add($str(R.string.simulation_two));
            add($str(R.string.simulation_three));
            add($str(R.string.simulation_four));
            add($str(R.string.simulation_five));
            add($str(R.string.simulation_six));
        }}));
        //随机
        traditionList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.random), new ArrayList<String>() {{
            add($str(R.string.random_one));
            add($str(R.string.random_two));
            add($str(R.string.random_three));
            add($str(R.string.random_four));
            add($str(R.string.random_five));
            add($str(R.string.random_six));
        }}));
    }

    {
        //磁震按摩
        modernList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.Magnetic), new ArrayList<String>() {{
            add($str(R.string.Magnetic_one));
            add($str(R.string.Magnetic_two));
            add($str(R.string.Magnetic_three));
            add($str(R.string.Magnetic_four));
        }}));
        //针灸拔刺
        modernList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.Acupuncture), new ArrayList<String>() {{
            add($str(R.string.Acupuncture_one));
            add($str(R.string.Acupuncture_two));
            add($str(R.string.Acupuncture_three));
            add($str(R.string.Acupuncture_four));
        }}));
        //火罐刮痧
        modernList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.Cupping), new ArrayList<String>() {{
            add($str(R.string.Cupping_one));
            add($str(R.string.Cupping_two));
            add($str(R.string.Cupping_three));
            add($str(R.string.Cupping_four));
        }}));
        //推拿抓捏
        modernList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.fashion), new ArrayList<String>() {{
            add($str(R.string.fashion_one));
            add($str(R.string.fashion_two));
            add($str(R.string.fashion_three));
            add($str(R.string.fashion_four));
        }}));
        //搓揉挤压
        modernList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.Knead), new ArrayList<String>() {{
            add($str(R.string.Knead_one));
            add($str(R.string.Knead_two));
            add($str(R.string.Knead_three));
            add($str(R.string.Knead_four));
        }}));
        //锤击敲打
        modernList.add(new SecondaryListAdapter.DataTree<String, String>($str(R.string.Hammer), new ArrayList<String>() {{
            add($str(R.string.Hammer_one));
            add($str(R.string.Hammer_two));
            add($str(R.string.Hammer_three));
            add($str(R.string.Hammer_four));
        }}));
    }


    //回调到界面，把点击的内容传递回去
    public static abstract class ListPopClickCallback {

        //回调选中的字
        public abstract void onSelect(String tvContent);
        //回调 选中的是现代理疗
        public abstract void chooseModern();
        //回调 选中的是现代理疗
        public abstract void chooseTradition();
    }

}
